/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.utils;


import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import pl.put.dcs_modeler.exceptions.DCSModelerException;
import pl.put.dcs_modeler.project.projectPropertiesExtensions.dcsProjectProperties.projectConfigModel.DCSServerConfig;
/**
 *
 * @author LukaszR
 */
public class DCSProjectUtil
{
    public static Logger log = Logger.getLogger( DCSProjectUtil.class.getName() );

    public static boolean isDCSProject( Project p )
    {

        Element dcsProjectConfiguration = ProjectUtils.getAuxiliaryConfiguration( p ).getConfigurationFragment( "dcsProjectConfiguration", "dcsProjectConfiguration", false );
        if ( dcsProjectConfiguration == null )
        {
            return false;
        }
        Element isDCS = ( Element )( dcsProjectConfiguration.getElementsByTagName( "isDCSProject" ).item( 0 ) );
        return Boolean.parseBoolean( isDCS.getAttribute( "val" ) );
    }

    public static String getDataModelPath( Project p )
    {
        Element dcsProjectConfiguration = ProjectUtils.getAuxiliaryConfiguration( p ).getConfigurationFragment( "dcsProjectConfiguration", "dcsProjectConfiguration", false );

        try
        {
            Element dataModel = ( Element )dcsProjectConfiguration.getElementsByTagName( "dataModel" ).item( 0 );
            return dataModel.getAttribute( "path" );
            
        }
        catch ( Exception e )
        {
            new DCSModelerException( "DCSProjectUtil.getDataModelPath", "Project configuration problem. No datamodel path defined" );
            e.printStackTrace();
        }
        return null;
    }

    public static List< DCSServerConfig > getServersConfig( Project p )
    {
        DCSServerConfig server = new DCSServerConfig();
        Element dcsProjectConfiguration = ProjectUtils.getAuxiliaryConfiguration( p ).getConfigurationFragment( "dcsProjectConfiguration", "dcsProjectConfiguration", false );
        NodeList nl = dcsProjectConfiguration.getElementsByTagName( "dcsServers" );
        if ( ( nl != null ) && ( nl.getLength() > 0 ) )
        {
            Element svr = ( Element )nl.item( 0 );
            NodeList svrs = svr.getElementsByTagName( "server" );
            if ( ( svrs != null ) && ( svrs.getLength() > 0 ) )
            {
                Element svrCfg = ( Element )svrs.item( 0 );
                server.setServerPath( svrCfg.getAttribute( "serverPath" ) );
            }
        }
        List< DCSServerConfig > ret = new ArrayList< DCSServerConfig >();
        ret.add( server );
        return ret;
    }

    public static void setServersConfig( Project p, List< DCSServerConfig > servers )
    {
        Element dcsProjectConfiguration = ProjectUtils.getAuxiliaryConfiguration( p ).getConfigurationFragment( "dcsProjectConfiguration", "dcsProjectConfiguration", false );
        Document doc = dcsProjectConfiguration.getOwnerDocument();
        NodeList nl = dcsProjectConfiguration.getElementsByTagName( "dcsServers" );
        Element svr;
        if ( ( nl == null ) || ( nl.getLength() == 0 ) )
        {
            svr = doc.createElement( "dcsServers" );
            dcsProjectConfiguration.appendChild( svr );
        }
        else
        {
            svr = ( Element )nl.item( 0 );
        }

        NodeList svrs = svr.getElementsByTagName( "server" );
        Element svrCfg;
        if ( ( svrs == null ) || ( svrs.getLength() == 0 ) )
        {
            svrCfg = doc.createElement( "server" );
            svr.appendChild( svrCfg );
        }
        else
        {
            svrCfg = ( Element )svrs.item( 0 );
        }
        svrCfg.setAttribute( "serverPath", servers.get( 0 ).getServerPath() );

        AuxiliaryConfiguration ac = ProjectUtils.getAuxiliaryConfiguration( p );
        ac.putConfigurationFragment( dcsProjectConfiguration, false );
    }

}
